
function initializeTinyWebDev()
{
    var oTm = document.getElementById("TimestampTxt");
    if( oTm.value.length < 1 )
    {
        oTm.value = new Date().getTime();
    }
}

function convertTimestampToDate()
{
    var oTm = document.getElementById("TimestampTxt");
    var tm = parseInt(oTm.value);
    console_log("Trying to convert " + oTm.value + " timestamp to datetime...");
    if( isNaN(tm) )
    {
        console_log("Bad timestamp, should be number!");
        return;
    }

    var fmt = document.getElementById("DateTimeFormatTxt").value;
    var dt = new Date(tm);
    console_log("Got datetime from timestamp: " + dt.toString());
    document.getElementById("DateTimeTxt").value = formatDateTimeString(dt, fmt);
}

function console_log(msg)
{
    var ta = document.getElementById("DebugTxt");
    if( ta != null )
    {
        ta.value = msg + "\n" + ta.value;
    }
}

// Available format is "yyyy-MM-dd HH:mm:ss"
function formatDateTimeString(dt, fmt)
{
    var s = fmt == null ? "yyyy-MM-dd" : fmt;
    var temp = "";

    temp = (1900 + dt.getYear());
    s = s.replace("yyyy", temp);

    temp = dt.getMonth() > 8 ? (dt.getMonth() + 1) : "0" + (dt.getMonth() + 1);
    s = s.replace("MM", temp);

//        temp = dt.getUTCDate() > 9 ? dt.getUTCDate() : "0" + dt.getUTCDate();
    temp = dt.getUTCDate() > 9 ? dt.getDate() : "0" + dt.getDate();
    s = s.replace("dd", temp);

    temp = dt.getHours() > 9 ? dt.getHours() : "0" + dt.getHours();
    s = s.replace("HH", temp);

    temp = dt.getMinutes() > 9 ? dt.getMinutes() : "0" + dt.getMinutes();
    s = s.replace("mm", temp);

    temp = dt.getSeconds() > 9 ? dt.getSeconds() : "0" + dt.getSeconds();
    s = s.replace("ss", temp);

    return s;
}

window.onload = function()
{
    initializeTinyWebDev();
    var oTm = document.getElementById("TimestampTxt");
    oTm.focus();
    oTm.onkeydown = function(ev)
    {
        console_log("Pressed: " + ev.keyCode);
        if( ev.keyCode == 13 )
        {
            ev.preventDefault();
            convertTimestampToDate();
        }else if( (ev.keyCode == 17) || (ev.keyCode == 86) )
        {
            // Ignore Ctrl + v
        }else if( (ev.keyCode < 96) || (ev.keyCode > 105) )
        {
            ev.preventDefault();
        }
    }
    oTm.select();
    document.getElementById("ConvertBtn").onclick = function()
    {
        convertTimestampToDate();
    }

    var versionTxt = document.getElementById("Version");
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function(){
        if (xhr.readyState == 4)
        {
            console_log("Got smth from LocalWebServer: " + xhr.responseText);
            // JSON.parse does not evaluate the attacker's scripts
            var resp = xhr.responseText.length > 0 ? JSON.parse(xhr.responseText) : {Status: "TIMEOUT"};
            if(resp.Status == "OK")
            {
                var v = resp.Major + "." + resp.Minor + "." + resp.Build;
                console_log("Version: " + v);
                versionTxt.value = v;
            }else
            {
                versionTxt.value = "ERROR!";
            }
        }
    }
    xhr.open("GET", "http://aleksejaus:9791/jira/", true);
    xhr.send();
}